package com.seeyon.apps.energy.manager;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.collaboration.api.CollaborationApi;
import com.seeyon.apps.collaboration.event.*;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.common.po.template.CtpTemplate;
import com.seeyon.ctp.common.template.manager.TemplateManager;
import com.seeyon.ctp.util.JDBCAgent;
import com.seeyon.ctp.util.annotation.ListenEvent;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author Guoan
 * @date 2023/6/8 - 15:44
 */
public class sewPlugin {
        private static Log log = CtpLogFactory.getLog(sewPlugin.class);

        private TemplateManager templateManager = (TemplateManager) AppContext.getBean("templateManager");
        private FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");

        private CtpTemplate templeteByTemplateNumberCC = templateManager.getTempleteByTemplateNumber("ccbxtbfp");//出差报销
        private CtpTemplate templeteByTemplateNumberXJ = templateManager.getTempleteByTemplateNumber("xjbxtbfp");//现金报销
        private CtpTemplate templeteByTemplateNumberYK = templateManager.getTempleteByTemplateNumber("ykbxtbfp");//用款报销


        private CtpTemplate xacgsqs = templateManager.getTempleteByTemplateNumber("cgsqxa");//西安采购申请书
        private CtpTemplate whcgsqs = templateManager.getTempleteByTemplateNumber("whcgsqs");//武汉采购申请书
        private CtpTemplate tjcgsqs = templateManager.getTempleteByTemplateNumber("cgsq01");//天津采购申请书
        private CtpTemplate tycgsqs = templateManager.getTempleteByTemplateNumber("tycgsqs");//太原采购申请书
        private CtpTemplate szcgsqs = templateManager.getTempleteByTemplateNumber("szcgsqs");//苏州采购申请书
        private CtpTemplate sycgsqs = templateManager.getTempleteByTemplateNumber("sycgsqs");//沈阳采购申请书
        private CtpTemplate gzcgsqs = templateManager.getTempleteByTemplateNumber("gzcgsqs");//广州采购申请书
        private CtpTemplate fscgsqs = templateManager.getTempleteByTemplateNumber("cgsqfs01");//佛山采购申请书

        public sewPlugin() {
        }
        @ListenEvent(event= CollaborationStartEvent.class)//流程发起事件
        public void onStart(CollaborationStartEvent event) throws Exception {
                log.info("同步发票状态流程发起进来啦！---------------------------------------");
                if(templeteByTemplateNumberCC.getId().equals(event.getAffair().getTempleteId())){//出差报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberCC.getId());
                        log.info("插件获取的模板id=" +event.getAffair().getTempleteId() );
                        log.info("出差报销进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 0, 0);
                        log.info("出差报销结束啦！--------------------------------");
                }else if(templeteByTemplateNumberXJ.getId().equals(event.getAffair().getTempleteId())){//现金报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberXJ.getId());
                        log.info("插件获取的模板id=" +event.getAffair().getTempleteId() );
                        log.info("现金报销进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 1, 0);
                        log.info("现金报销结束啦！--------------------------------");
                }else if(templeteByTemplateNumberYK.getId().equals(event.getAffair().getTempleteId())){//用款报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getAffair().getTempleteId() );
                        log.info("用款报销进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 2, 0);
                        log.info("用款报销结束啦！--------------------------------");
                }
                log.info("同步发票状态流程发起结束啦！---------------------------------------");
        }

        @ListenEvent(event= CollaborationAppointStepBackEvent.class)//流程指定回退事件
        public void onStart(CollaborationAppointStepBackEvent event) throws Exception {
                log.info("同步发票状态流程指定回退进来啦！---------------------------------------");
                if(templeteByTemplateNumberCC.getId().equals(event.getTemplateId())){//出差报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberCC.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("出差报销进来啦！--------------------------------");
                        this.getData(event.getCurrentAffair().getFormRecordid(), 0, 1);
                        log.info("出差报销结束啦！--------------------------------");
                }else if(templeteByTemplateNumberXJ.getId().equals(event.getTemplateId())){//现金报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberXJ.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("现金报销进来啦！--------------------------------");
                        this.getData(event.getCurrentAffair().getFormRecordid(), 1, 1);
                        log.info("现金报销结束啦！--------------------------------");
                }else if(templeteByTemplateNumberYK.getId().equals(event.getTemplateId())){//用款报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("用款报销进来啦！--------------------------------");
                        this.getData(event.getCurrentAffair().getFormRecordid(), 2, 1);
                        log.info("用款报销结束啦！--------------------------------");
                }else if (xacgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("西安采购申请书进来啦！--------------------------------");
                        this.getData(event.getCurrentAffair().getFormRecordid(), 3, 1);
                        log.info("西安采购申请书结束啦！--------------------------------");
                }else if (whcgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("武汉采购申请书进来啦！--------------------------------");
                        this.getData(event.getCurrentAffair().getFormRecordid(), 4, 1);
                        log.info("武汉采购申请书结束啦！--------------------------------");
                }else if (tjcgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("天津采购申请书进来啦！--------------------------------");
                        this.getData(event.getCurrentAffair().getFormRecordid(), 5, 1);
                        log.info("天津采购申请书结束啦！--------------------------------");
                }else if(tycgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("太原采购申请书进来啦！--------------------------------");
                        this.getData(event.getCurrentAffair().getFormRecordid(), 6, 1);
                        log.info("太原采购申请书结束啦！--------------------------------");
                }else if(szcgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("苏州采购申请书进来啦！--------------------------------");
                        this.getData(event.getCurrentAffair().getFormRecordid(), 7, 1);
                        log.info("苏州采购申请书结束啦！--------------------------------");
                }else if (sycgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("沈阳采购申请书进来啦！--------------------------------");
                        this.getData(event.getCurrentAffair().getFormRecordid(), 8, 1);
                        log.info("沈阳采购申请书结束啦！--------------------------------");
                }else if (gzcgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("广州采购申请书进来啦！--------------------------------");
                        this.getData(event.getCurrentAffair().getFormRecordid(), 9, 1);
                        log.info("广州采购申请书结束啦！--------------------------------");
                }else if (fscgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("佛山采购申请书进来啦！--------------------------------");
                        this.getData(event.getCurrentAffair().getFormRecordid(), 10, 1);
                        log.info("佛山采购申请书结束啦！--------------------------------");
                }
                log.info("同步发票状态流程指定回退结束啦！---------------------------------------");
        }

        @ListenEvent(event= CollaborationFinishEvent.class)//流程结束事件
        public void onStart(CollaborationFinishEvent event) throws Exception {
                log.info("同步发票状态流程结束进来啦！---------------------------------------");
                if(templeteByTemplateNumberCC.getId().equals(event.getAffair().getTempleteId())){//出差报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberCC.getId());
                        log.info("插件获取的模板id=" +event.getAffair().getTempleteId());
                        log.info("出差报销进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 0, 2);
                        log.info("出差报销结束啦！--------------------------------");
                }else if(templeteByTemplateNumberXJ.getId().equals(event.getAffair().getTempleteId())){//现金报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberXJ.getId());
                        log.info("插件获取的模板id=" +event.getAffair().getTempleteId());
                        log.info("现金报销进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 1, 2);
                        log.info("现金报销结束啦！--------------------------------");
                }else if(templeteByTemplateNumberYK.getId().equals(event.getAffair().getTempleteId())){//用款报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getAffair().getTempleteId());
                        log.info("用款报销进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 2, 2);
                        log.info("用款报销结束啦！--------------------------------");
                }
                log.info("同步发票状态流程结束结束啦！---------------------------------------");
        }

        @ListenEvent(event= CollaborationCancelEvent.class)//流程撤销事件
        public void onStart(CollaborationCancelEvent event) throws Exception {
                log.info("同步发票状态流程撤销进来啦！---------------------------------------");
                if(templeteByTemplateNumberCC.getId().equals(event.getTemplateId())){//出差报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberCC.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId());
                        log.info("出差报销进来啦！--------------------------------");
                        this.getData(event.getSummaryId(), 0, 3);
                        log.info("出差报销结束啦！--------------------------------");
                }else if(templeteByTemplateNumberXJ.getId().equals(event.getTemplateId())){//现金报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberXJ.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId());
                        log.info("现金报销进来啦！--------------------------------");
                        this.getData(event.getSummaryId(), 1, 3);
                        log.info("现金报销结束啦！--------------------------------");
                }else if(templeteByTemplateNumberYK.getId().equals(event.getTemplateId())){//用款报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId());
                        log.info("用款报销进来啦！--------------------------------");
                        this.getData(event.getSummaryId(), 2, 3);
                        log.info("用款报销结束啦！--------------------------------");
                }
                log.info("同步发票状态流程撤销结束啦！---------------------------------------");
        }

        @ListenEvent(event= CollaborationStepBackEvent.class)//流程回退事件
        public void onStart(CollaborationStepBackEvent event) throws Exception {
                log.info("同步发票状态流程回退进来啦！---------------------------------------");
                if(templeteByTemplateNumberCC.getId().equals(event.getAffair().getTempleteId())){//出差报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberCC.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId());
                        log.info("出差报销进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 0, 4);
                        log.info("出差报销结束啦！--------------------------------");
                }else if(templeteByTemplateNumberXJ.getId().equals(event.getAffair().getTempleteId())){//现金报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberXJ.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId());
                        log.info("现金报销进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 1, 4);
                        log.info("现金报销结束啦！--------------------------------");
                }else if(templeteByTemplateNumberYK.getId().equals(event.getAffair().getTempleteId())){//用款报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId());
                        log.info("用款报销进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 2, 4);
                        log.info("用款报销结束啦！--------------------------------");
                }else if (xacgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("西安采购申请书进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 3, 4);
                        log.info("西安采购申请书结束啦！--------------------------------");
                }else if (whcgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("武汉采购申请书进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 4, 4);
                        log.info("武汉采购申请书结束啦！--------------------------------");
                }else if (tjcgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("天津采购申请书进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 5, 4);
                        log.info("天津采购申请书结束啦！--------------------------------");
                }else if(tycgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("太原采购申请书进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 6, 4);
                        log.info("太原采购申请书结束啦！--------------------------------");
                }else if(szcgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("苏州采购申请书进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 7, 4);
                        log.info("苏州采购申请书结束啦！--------------------------------");
                }else if (sycgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("沈阳采购申请书进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 8, 4);
                        log.info("沈阳采购申请书结束啦！--------------------------------");
                }else if (gzcgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("广州采购申请书进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 9, 4);
                        log.info("广州采购申请书结束啦！--------------------------------");
                }else if (fscgsqs.getId().equals(event.getTemplateId())){
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId() );
                        log.info("佛山采购申请书进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 10, 4);
                        log.info("佛山采购申请书结束啦！--------------------------------");
                }
                log.info("同步发票状态流程回退结束啦！---------------------------------------");
        }

        @ListenEvent(event= CollaborationStopEvent.class)//流程终止事件
        public void onStart(CollaborationStopEvent event) throws Exception {
                log.info("同步发票状态流程终止进来啦！---------------------------------------");
                if(templeteByTemplateNumberCC.getId().equals(event.getAffair().getTempleteId())){//出差报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberCC.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId());
                        log.info("出差报销进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 0, 5);
                        log.info("出差报销结束啦！--------------------------------");
                }else if(templeteByTemplateNumberXJ.getId().equals(event.getAffair().getTempleteId())){//现金报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberXJ.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId());
                        log.info("现金报销进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 1, 5);
                        log.info("现金报销结束啦！--------------------------------");
                }else if(templeteByTemplateNumberYK.getId().equals(event.getAffair().getTempleteId())){//用款报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId());
                        log.info("用款报销进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 2, 5);
                        log.info("用款报销结束啦！--------------------------------");
                }
                log.info("同步发票状态流程终止结束啦！---------------------------------------");
        }

        @ListenEvent(event= CollaborationProcessEvent.class)//流程处理事件
        public void onStart(CollaborationProcessEvent event) throws Exception {
                log.info("同步发票状态流程处理进来啦！---------------------------------------");
                if(templeteByTemplateNumberCC.getId().equals(event.getAffair().getTempleteId()) && "agree".equals(event.getComment().getExtAtt4())){//出差报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberCC.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId());
                        log.info("出差报销同意进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 0, 6);
                        log.info("出差报销同意结束啦！--------------------------------");
                }else if(templeteByTemplateNumberXJ.getId().equals(event.getAffair().getTempleteId()) && "agree".equals(event.getComment().getExtAtt4())){//现金报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberXJ.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId());
                        log.info("现金报销同意进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 1, 6);
                        log.info("现金报销同意结束啦！--------------------------------");
                }else if(templeteByTemplateNumberYK.getId().equals(event.getAffair().getTempleteId()) && "agree".equals(event.getComment().getExtAtt4())){//用款报销
                        log.info("通过模板编号获取的模板id="+ templeteByTemplateNumberYK.getId());
                        log.info("插件获取的模板id=" +event.getTemplateId());
                        log.info("用款报销同意进来啦！--------------------------------");
                        this.getData(event.getAffair().getFormRecordid(), 2, 6);
                        log.info("用款报销同意结束啦！--------------------------------");
                }
                log.info("同步发票状态流程终止结束啦！---------------------------------------");
        }


        /**
         *
         * @param id 数据id
         * @param status  0-出差 1-现金 2-用款 3-西安采购申请书  4-武汉采购申请书 5-天津采购申请书 6-太原采购申请书 7-苏州采购申请书 8-沈阳采购申请书 9-广州采购申请书 10-佛山采购申请书
         * @param state  流程状态 0-流程发起 1-流程指定回退事件 2-流程结束事件  3-流程撤销事件 4-流程回退事件 5-流程终止事件
         * @return 返回组装好的数据
         */
        private void getData(Long id,int status,int state) throws IOException {
                JDBCAgent jdbc = new JDBCAgent();
                JSONArray dataList=new JSONArray();
                JSONObject obj=new JSONObject();
                JSONArray invoiceList=new JSONArray();
                ResultSet result = null;
                String memberid = null;//个人代办的代办人id
                String fromUserid = null;//表单上的发起人id
                String activityid = null;//流程节点id
                String bu = null; //公司编码
                try{
                        if(state == 1 || state == 4){
                                String sqlss = "select member_id,activity_id from ctp_affair where state = '3' and sub_state = '16' and form_recordid = '" + id + "'";
                                jdbc.execute(sqlss);
                                Map affairmap = jdbc.resultSetToMap();
                                if(affairmap.get("member_id") != null && !affairmap.get("member_id").toString().isEmpty()){
                                        memberid = affairmap.get("member_id").toString();
                                        log.info(affairmap.get("activity_id"));
                                        if(affairmap.get("activity_id") != null){
                                                activityid = affairmap.get("activity_id").toString();
                                        }
                                }else{
                                        String sqls = "select member_id,activity_id from ctp_affair where state = '3' and (sub_state = '2' or sub_state = '7') and form_recordid = '" + id + "'";
                                        jdbc.execute(sqls);
                                        Map affairmap1 = jdbc.resultSetToMap();
                                        memberid = affairmap1.get("member_id").toString();
                                        log.info(affairmap.get("activity_id"));
                                        if(affairmap1.get("activity_id") != null){
                                                activityid = affairmap1.get("activity_id").toString();
                                        }
                                }
                                log.info("流程节点activity_id="+activityid);

                        }else if(state == 3){
                                String summarysql = "select FORM_RECORDID from COL_SUMMARY where id = '" + id + "'";
                                jdbc.execute(summarysql);
                                Map map1 = jdbc.resultSetToMap();
                                id = Long.valueOf(map1.get("FORM_RECORDID").toString());
                        }
                        String sql = "";//主表sql
                        String zbsql = "";//子表sql 查询发票id
                        if(status == 0 || status == 1 || status == 2){
                                if(status == 0){
                                        sql = AppContext.getSystemProperty("sew.ccsql") + " '" + id + "'";//获取出差报销sql
                                        zbsql = AppContext.getSystemProperty("sew.ccZbsqlOne") + " '" + id + "' "
                                                + AppContext.getSystemProperty("sew.ccZbsqlTwo") + " '" + id + "' "
                                                + AppContext.getSystemProperty("sew.ccZbsqlThree") + " '" + id + "'";//查询出差发票id
                                }else if(status == 1){
                                        sql = AppContext.getSystemProperty("sew.xjsql") + " '" + id + "'";//获取现金报销sql
                                        zbsql = AppContext.getSystemProperty("sew.xjZbsql") + " '" + id + "'";//查询现金发票id
                                }else if(status == 2){
                                        sql = AppContext.getSystemProperty("sew.yksql") + " '" + id + "'";//获取用款报销sql
                                        zbsql = AppContext.getSystemProperty("sew.ykZbsql") + " '" + id + "'";//查询用款发票id
                                }
                                jdbc.execute(sql);
                                Map map = jdbc.resultSetToMap();
                                log.info("查询sql返回的结果=" + map);
                                obj.put("userId", map.get("员工工号"));//用户编码
                                obj.put("agencyCode", map.get("税号"));//报销单位税号
                                obj.put("agencyName", map.get("所属单位"));//报销单位名称
                                obj.put("reimbursementId", map.get("单据编号"));//报销单号
                                obj.put("flowCode", nullToObject(map.get("单据编号")));//报销单号
                                obj.put("id", map.get("id"));
                                fromUserid = map.get("发起人").toString();
                                jdbc.execute(zbsql);
                                List list = jdbc.resultSetToList();
                                log.info("查询子表元素=" + list.size());
                                if(list.size() > 0){
                                        for(int i = 0; i < list.size(); i++){
                                                Map entityMap = (Map)list.get(i);
                                                log.info("子表数据=" + entityMap.toString());
                                                log.info("发票id=" + entityMap.get("invoiceid"));
                                                JSONObject entityObj=new JSONObject();
                                                if(entityMap.get("invoiceid")!=null){
                                                        entityObj.put("invoiceId", entityMap.get("invoiceid").toString());
                                                        invoiceList.add(entityObj);
                                                }
                                        }
                                }
                                obj.put("invoiceList", invoiceList);
                        }else if(status == 3 || status == 4 || status == 5 || status == 6 || status == 7 || status == 8 || status == 9 || status == 10){
                                if(status == 3){
                                        sql = AppContext.getSystemProperty("sew.xaZbsql") + " '" + id + "'";//西安
                                }else if(status == 4){
                                        sql = AppContext.getSystemProperty("sew.whZbsql") + " '" + id + "'";//武汉
                                }else if(status == 5){
                                        sql = AppContext.getSystemProperty("sew.tjZbsql") + " '" + id + "'";//天津
                                }else if(status == 6){
                                        sql = AppContext.getSystemProperty("sew.tyZbsql") + " '" + id + "'";//太原
                                }else if(status == 7){
                                        sql = AppContext.getSystemProperty("sew.szZbsql") + " '" + id + "'";//苏州
                                }else if(status == 8){
                                        sql = AppContext.getSystemProperty("sew.syZbsql") + " '" + id + "'";//沈阳
                                }else if(status == 9){
                                        sql = AppContext.getSystemProperty("sew.gzZbsql") + " '" + id + "'";//广州
                                }else if(status == 10){
                                        sql = AppContext.getSystemProperty("sew.fsZbsql") + " '" + id + "'";//佛山
                                }

                                jdbc.execute(sql);
                                Map map = jdbc.resultSetToMap();
                                fromUserid = map.get("发起人").toString();
                                bu = map.get("公司编码").toString();
                        }

                }catch(Exception e){
                        log.error(e.getMessage(), e);
                }finally {
                        if(result!=null) {
                                try {
                                        result.close();
                                } catch (SQLException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                        jdbc.close();
                }
                if(status == 0 || status == 1 || status == 2){
                        if(state == 0){
                                obj.put("reimbursementStatus", "1");//报销中
                                dataList.add(obj);
                                this.sendFp(dataList);
                        }else if(state == 1){
                                if(memberid != null && fromUserid != null  && memberid.equals(fromUserid)){
                                        obj.put("reimbursementStatus", "0");//未报销
                                        dataList.add(obj);
                                        this.sendFp(dataList);
                                }
                        }else if(state == 2){
                                obj.put("reimbursementStatus", "2");//已报销
                                dataList.add(obj);
                                this.sendFp(dataList);
                        }else if(state == 3){
                                obj.put("reimbursementStatus", "0");//未报销
                                dataList.add(obj);
                                this.sendFp(dataList);
                        }else if(state == 4){
                                if(memberid != null && fromUserid != null  && memberid.equals(fromUserid)){
                                        obj.put("reimbursementStatus", "0");//未报销
                                        dataList.add(obj);
                                        this.sendFp(dataList);
                                }
                        }else if(state == 5){
                                obj.put("reimbursementStatus", "0");//未报销
                                dataList.add(obj);
                                this.sendFp(dataList);
                        }else if(state == 6){
                                obj.put("reimbursementStatus", "1");//未报销
                                dataList.add(obj);
                                this.sendFp(dataList);
                        }
                }else if(status == 3 || status == 4 || status == 5 || status == 6 || status == 7 || status == 8 || status == 9 || status == 10){
                      if(activityid == null ){
                              this.sendCg(id,bu);
                      }
                }

        }
        /**
         * 调用接口同步发票状态
         * @param dataList 组装好的数据
         * @throws IOException
         */
        private void sendFp(JSONArray dataList) throws IOException {
                log.info("发送data参数" + dataList.getJSONObject(0).toString());
                String url = AppContext.getSystemProperty("sew.fpUrl");
                SimpleDateFormat sf=new SimpleDateFormat("yyyyMMddHHmmss");
                //组装参数
                JSONObject param=new JSONObject();
                param.put("version", "1.0");
                param.put("nonce", new Date().getTime());
                param.put("timestamp", sf.format(new Date()));
                param.put("data", dataList.getJSONObject(0).toString().replaceAll("\"", "\\\""));

                //请求接口 更新发票报销状态
                HttpClient httpclient = new HttpClient();
                PostMethod method = new PostMethod(url);
                method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
                method.setRequestHeader("Content-Type","application/json;charset=UTF-8");
                RequestEntity se=new StringRequestEntity(param.toString(),"application/json" ,"UTF-8");
                log.info("传送接口参数{}" + param);
                method.setRequestEntity(se);
                httpclient.executeMethod(method);
                JSONObject obj = JSONObject.parseObject(method.getResponseBodyAsString());
                log.info("单据编号:"+dataList.getJSONObject(0).getString("flowCode")+",状态:"+dataList.getJSONObject(0).getString("reimbursementStatus")+",结果:"+obj);
        }

        public String nullToObject(Object obj){
                if(obj==null)
                        return "";
                else
                        return obj+"";
        }

        private void sendCg(Long fromRecordid,String bu){
                String url = AppContext.getSystemProperty("sew.cgUrl") + "?id=" + fromRecordid + "&bu=" + bu;
                org.apache.http.client.HttpClient client = HttpClientBuilder.create().build();
                HttpGet request = new HttpGet(url);
                request.setHeader("Content-Type","application/json");
                String result=null;
                try {
                        HttpResponse response = client.execute(request);
                        HttpEntity entity = response.getEntity();
                        result= EntityUtils.toString(entity,"utf-8");
                } catch (IOException e) {
                        e.printStackTrace();
                }
                log.info("返回的信息" + result);
        }

}
